package com.zhuozhou.jdyc.main.goods.activity;

import android.os.Bundle;
import android.view.View;

import com.zhuozhou.jdyc.R;
import com.zhuozhou.jdyc.base.BaseActivity;
import com.zhuozhou.jdyc.databinding.ActivityEvaluateBinding;
import com.zhuozhou.jdyc.main.goods.adapter.EvaluateAdapter;
import com.zhuozhou.jdyc.main.goods.bean.BasicsCommentDetailsBase;
import com.zhuozhou.jdyc.main.goods.viewmodel.GoodsViewModel;
import com.zhuozhou.jdyc.wight.recyclerview.MyLayoutManager;

import java.util.ArrayList;
import java.util.List;

/**
 * Create by Once_ChuXy on 2021/9/17
 * Describe:评价
 */
public class EvaluateActivity extends BaseActivity<GoodsViewModel, ActivityEvaluateBinding> {
    private EvaluateAdapter evaluateAdapter;
    private List<BasicsCommentDetailsBase.CommentBean> hpList = new ArrayList<>();
    private List<BasicsCommentDetailsBase.CommentBean> cpList = new ArrayList<>();
    private List<BasicsCommentDetailsBase.CommentBean> zpList = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_evaluate;
    }

    @Override
    public String setTitleBar() {
        return "评价列表";
    }

    @Override
    public void initView() {
        mViewModel.BasicsCommentDetailsMutableLiveData.observe(this, v -> {
            List<String> labels = new ArrayList<>();
            labels.add("全部(" + (v.getChaping() + v.getHaoping() + v.getZhongping()) + ")");
            labels.add("好评(" + v.getHaoping() + ")");
            labels.add("中评(" + v.getZhongping() + ")");
            labels.add("差评(" + v.getChaping() + ")");
            binding.labels.setLabels(labels);
            setEvaluate(v.getComment());
            hpList.clear();
            zpList.clear();
            cpList.clear();
            for (BasicsCommentDetailsBase.CommentBean item : v.getComment()) {
                if (Integer.parseInt(item.getRate()) > 3) {
                    hpList.add(item);
                } else if (Integer.parseInt(item.getRate()) == 3) {
                    zpList.add(item);
                } else if (Integer.parseInt(item.getRate()) < 3) {
                    cpList.add(item);
                }
            }
            binding.labels.setOnLabelClickListener((label, data, position) -> {
                switch (position) {
                    case 0:
                        setEvaluate(v.getComment());
                        break;
                    case 1:
                        setEvaluate(hpList);
                        break;
                    case 2:
                        setEvaluate(zpList);
                        break;
                    case 3:
                        setEvaluate(cpList);
                        break;
                }
            });
        });
    }

    public void setEvaluate(List<BasicsCommentDetailsBase.CommentBean> list) {
        if (list != null && list.size() != 0) {
            binding.tvNoEvaluate.setVisibility(View.GONE);
            binding.mRecyclerViewEvaluate.setVisibility(View.VISIBLE);
            evaluateAdapter = new EvaluateAdapter(this);
            evaluateAdapter.setList(list);
            binding.mRecyclerViewEvaluate.setAdapter(evaluateAdapter);
            binding.mRecyclerViewEvaluate.setLayoutManager(new MyLayoutManager(this));
        } else {
            binding.tvNoEvaluate.setVisibility(View.VISIBLE);
            binding.mRecyclerViewEvaluate.setVisibility(View.GONE);
        }
    }

    @Override
    public void initData(Bundle bundle) {
        mViewModel.getBasicsCommentDetails(bundle.getInt("id"), bundle.getInt("type"));
    }
}
